package it.polimi.swim.session;

import it.polimi.swim.entity.Skill;
import it.polimi.swim.entity.SystemUser;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.ejb3.annotation.RemoteBinding;

@Stateless
@RemoteBinding(jndiBinding = SkillManagerBean.REMOTE)
public class SkillManagerBean implements SkillManagerBeanRemote {

	public static final String REMOTE = "SkillManagerBean/remote";

	@PersistenceContext(unitName = "SWIMv2")
	private EntityManager em;

	public SkillManagerBean() {

	}

	@SuppressWarnings("unchecked")
	public List<Skill> availableSkills() {
		Query q = null;

		List<Skill> skillList;

		q = em.createNamedQuery("Skill.getAllSkills");
		skillList = q.getResultList();
		System.out.println("query"+skillList.size());
		return skillList;
	}

	@Override
	public Skill createSkill(String skill) {
		System.out.println("skill letta: "+skill);
		Skill s = new Skill();
		s.setName(skill);
		s.setStatus(true);
		em.persist(s);
		return s;
	}

	@Override
	public List<Skill> getUncheckedSkill() {
		Query q;

		List<Skill> skillList = new ArrayList<Skill>();

		q = em.createNamedQuery("Skill.getNotRegisteredSkill");
		skillList = (ArrayList<Skill>) q.getResultList();
		System.out.println("size "+skillList.size());
		return skillList;
	}

	@Override
	public void validateSkill(String skill) {
		Query q = null;
		List<Skill> skillList = new ArrayList<Skill>();

		q = em.createNamedQuery("Skill.getSkillById");
		q.setParameter("id", Integer.parseInt(skill));
		Skill skillUpdate = (Skill) q.getSingleResult();
		skillUpdate.setStatus(true);
		em.persist(skillUpdate);
		
	}

	@Override
	public Skill getSkillById(int idSkill) {
		Query q;
		Skill s;
		q = em.createNamedQuery("Skill.getSkillById");
		q.setParameter("id", idSkill);
		s = (Skill) q.getSingleResult();
		return s;
	}

	@Override
	public void sendSkillRequest(String skill) {
		Skill s = new Skill();
		s.setName(skill);
		s.setStatus(false);
		em.persist(s);
	}

}